package mta.io.csv.writer;

import au.com.bytecode.opencsv.CSVWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Created by Dmitry Nikolaev.
 * User: dima
 * Date: 22.01.2009
 * Time: 18:07:28
 */
public class CsvTranslationWriter {
    private String fileName;

    public CsvTranslationWriter(String fileName) {
        this.fileName = fileName;
    }

    public void writePhrases(HashMap<String, String> phrases) {
        SortedSet<String> sorted = new TreeSet<String>(phrases.keySet());

        CSVWriter writer;
        try {
            writer = new CSVWriter(new FileWriter(fileName));
            for (String original : sorted) {
                String translation = phrases.get(original);
                if (translation == null) {
                    translation = original;
                }
                String[] row = {original, translation};
                writer.writeNext(row);
            }
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
